/**
 * 
 */
package com.wskj.library.book.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wskj.library.pojo.BookCategory;
import com.wskj.library.pojo.BookInfo;
import com.wskj.library.service.BookService;
import com.wskj.library.service.impl.BookServiceImpl;
import com.wskj.util.Page;
import com.wskj.util.StringUtil;

/**
 * @author 胡洋
 * @describe 进行 书籍信息的显示模块
 * @Date 2018年3月22日下午6:08:54
 * @version 1.0
 */
@WebServlet(urlPatterns = "/book/list.do")
public class BookListServlet extends HttpServlet {
	//图书的服务类......
	private BookService bookService=new BookServiceImpl();
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BookCategory> categories=	bookService.queryAllCategory();		
	    req.setAttribute("ca",categories);
		String bname =StringUtil.nullToString( req.getParameter("bname"));//书本名称
		String author =StringUtil.nullToString( req.getParameter("author"));//作者
		int cid=StringUtil.nullToMinusInt(req.getParameter("cid")); // 默认为 -1
		int status=StringUtil.nullToMinusInt(req.getParameter("status"));//默认为 -1
		int index=StringUtil.nullToInt(req.getParameter("pageIndex")); //当前页面为1
				
		Page<BookInfo>  page=new Page<BookInfo>(index, 10);
		
		long counts=bookService.getCountsByBname(bname,author,cid,status);
		page.setCounts(counts);		
		
		List<BookInfo> books=bookService.getBooksWithPage(bname,author,cid,status,index,page.getPageSize());
		page.setData(books);
		req.setAttribute("page", page);		
		req.getRequestDispatcher("/WEB-INF/page/book/list.jsp").forward(req, resp);
		
	}

	

}
